package cn.ljy.uem.service.impl;


import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.model.SysDepartment;
import cn.ljy.authority.model.SysUser;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.*;
import cn.ljy.uem.model.*;
import cn.ljy.uem.model.query.SchoolTeachingTaskQuery;
import cn.ljy.uem.service.SchoolTeachingTaskService;
import cn.ljy.uem.service.common.CommonSchoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * @description 教学任务模块的业务层接口
 * @author 廖振豪
 * @date 2022-7-18 15:22
 */
@Service
@Slf4j
public class ISchoolTeachingTaskService extends IBaseService<SchoolTeachingTask, SchoolTeachingTaskQuery> implements SchoolTeachingTaskService {

    @Autowired
    private SchoolTeachingTaskMapper mapper;

    @Autowired
    private SchoolCourseMapper schoolCourseMapper;

    @Autowired
    private SchoolTermMapper schoolTermMapper;

    @Autowired
    private SchoolClassMapper schoolClassMapper;



    @Autowired
    private SchoolClassRoomMapper schoolClassRoomMapper;

    @Autowired
    private SchoolTeacherMapper schoolTeacherMapper;

    @Autowired
    private SchoolTeachingTaskTeacherMapper schoolTeachingTaskTeacherMapper;

    @Autowired
    private SchoolStudentMapper schoolStudentMapper;

    @Autowired
    private SchoolTeachingTaskStudentMapper schoolTeachingTaskStudentMapper;

    @Autowired
    private CommonSchoolService commonSchoolService;

    @Override
    protected void beforeGet(Serializable id) {
        if(mapper.findById(id)==null){
            throw new MyWebException("查找失败：找不到指定记录");
        }
    }

    @Override
    protected void beforeUpdate(SchoolTeachingTask schoolTeachingTask) throws MyWebException {
        if(mapper.findById(schoolTeachingTask.getId())==null){
            throw new MyWebException("编辑失败：找不到指定记录");
        }
//        /**
//         * 重新处理教学任务和教师的关系。
//         * 考虑到关系表没有自增id，且教学任务和教师关系的数量不会多，本来可以简化逻辑：先全部删除该教学任务所有教师关系，再重新添加
//         * 但为了保证任务和教师关系的建立时间可查，故采用规范的判断
//         */
//        if (schoolTeachingTask.getTeacherIds() != null || schoolTeachingTask.getTeacherIds().length != 0) {
//            //将前端传递过来的集合转换为set集合,顺便去重
//            Set<Long> teacherIdsSet = new HashSet<Long>(Arrays.asList(schoolTeachingTask.getTeacherIds()));
//            //获取所有的教师
//            List<SchoolTeacher> teachersOfAll = schoolTeacherMapper.findAll(null);
//            System.out.println(teachersOfAll);
//            //获取原来的教师
//            List<SchoolTeacher> teachersOrigin=schoolTeachingTaskTeacherMapper.findAllTeacherByTeachingTaskId(schoolTeachingTask.getId());
//            //处理教学任务和教师的关系
//            for(SchoolTeacher schoolTeacher:teachersOfAll){
//                if(teacherIdsSet.contains(schoolTeacher.getId())){//如果前端勾选了该教师
//                    if(!teachersOrigin.contains(schoolTeacher)){//如果该教师原来不存在
//                        //添加该教师关系
//                        schoolTeachingTaskTeacherMapper.save(schoolTeachingTask.getId(),schoolTeacher.getId(),2);
//                    }
//                }else{//如果没有勾选该教师
//                    if(teachersOrigin.contains(schoolTeacher)) {//如果该教师原来存在
//                        //移除该教师关系
//                        schoolTeachingTaskTeacherMapper.delete(schoolTeachingTask.getId(),schoolTeacher.getId());
//                    }
//                }
//            }
//        }
//        if(schoolTeachingTask.getStudentIds() != null || schoolTeachingTask.getStudentIds().length != 0) {
//            //将前端传递过来的集合转换为set集合,顺便去重
//            Set<Long> studentIdsSet = new HashSet<Long>(Arrays.asList(schoolTeachingTask.getStudentIds()));
//            //获取所有的教师
//            List<SchoolStudent> studentsOfAll = schoolStudentMapper.findAll(null);
////            System.out.println(studentsOfAll);
//            //获取原来的教师
//            List<SchoolStudent> studentsOrigin=schoolTeachingTaskStudentMapper.findAllStudentByTeachingTaskId(schoolTeachingTask.getId());
//            //处理教学任务和教师的关系
//            for(SchoolStudent schoolStudent:studentsOfAll){
//                if(studentIdsSet.contains(schoolStudent.getId())){//如果前端勾选了该教师
//                    if(!studentsOrigin.contains(schoolStudent)){//如果该教师原来不存在
//                        //添加该教师关系
//                        schoolTeachingTaskStudentMapper.save(schoolTeachingTask.getId(),schoolStudent.getId(),1);
//                    }
//                }else{//如果没有勾选该教师
//                    if(studentsOrigin.contains(schoolStudent)) {//如果该教师原来存在
//                        //移除该教师关系
//                        schoolTeachingTaskStudentMapper.delete(schoolTeachingTask.getId(),schoolStudent.getId());
//                    }
//                }
//            }
//        }

    }

    @Override
    protected void beforeSave(SchoolTeachingTask schoolTeachingTask) {
        //1.验证是否有该课程的管理权限
        //验证当前登录账户是否有该课程的权限
        commonSchoolService.validateCourseManageAuthorityOfLoginTeacher(schoolTeachingTask.getCourseId());
        //获取当前登录教师
        SchoolTeacher schoolTeacher = commonSchoolService.getLoginTeacher();
        //将当前登录教师设置为该教学任务创建人
        if(schoolTeacher!=null ){
            schoolTeachingTask.setTeacherId(schoolTeacher.getId());
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        schoolTeachingTaskTeacherMapper.deleteByTeachingTaskIds(ids);
        schoolTeachingTaskStudentMapper.deleteByTeachingTaskIds(ids);
    }


    @Override
    protected void afterGet(SchoolTeachingTask schoolTeachingTask) {
        //添加课程关联属性
        if (schoolTeachingTask.getCourseId() != null) {
            //查找教学任务所属课程
            SchoolCourse schoolCourse = schoolCourseMapper.findById(schoolTeachingTask.getCourseId());
            //赋值
            schoolTeachingTask.setSchoolCourse(schoolCourse);
        }
        if (schoolTeachingTask.getTeacherId() != null){
            schoolTeachingTask.setSchoolTeacher(schoolTeacherMapper.findById(schoolTeachingTask.getTeacherId()));
        }
        //添加学期关联属性
        if (schoolTeachingTask.getTermId() != null) {
            //查找教学任务所属课程
            SchoolTerm schoolTerm = schoolTermMapper.findById(schoolTeachingTask.getTermId());
            //赋值
            schoolTeachingTask.setSchoolTerm(schoolTerm);
        }
        //添加班级关联属性
        if (schoolTeachingTask.getClassId() != null) {
            //查找教学任务所属课程
            SchoolClass schoolClass = schoolClassMapper.findById(schoolTeachingTask.getClassId());
            //赋值
            schoolTeachingTask.setSchoolClass(schoolClass);
        }

        //添加教室关联属性
        if (schoolTeachingTask.getClassRoomId() != null) {
            //查找教学任务所属课程
            SchoolClassRoom schoolClassRoom = schoolClassRoomMapper.findById(schoolTeachingTask.getClassRoomId());
            //赋值
            schoolTeachingTask.setSchoolClassRoom(schoolClassRoom);
        }
    }

    @Override
    protected void afterFindAll(List<SchoolTeachingTask> list) {
        for (SchoolTeachingTask schoolTeachingTask : list) {
            //添加课程关联属性
            if (schoolTeachingTask.getCourseId() != null) {
                //查找教学任务所属课程
                SchoolCourse schoolCourse = schoolCourseMapper.findById(schoolTeachingTask.getCourseId());
                //赋值
                schoolTeachingTask.setSchoolCourse(schoolCourse);
            }
            //添加教师关联属性
            if (schoolTeachingTask.getTeacherId() != null){
                schoolTeachingTask.setSchoolTeacher(schoolTeacherMapper.findById(schoolTeachingTask.getTeacherId()));
            }
            //添加学期关联属性
            if (schoolTeachingTask.getTermId() != null) {
                //查找教学任务所属学期
                SchoolTerm schoolTerm = schoolTermMapper.findById(schoolTeachingTask.getTermId());
                //赋值
                schoolTeachingTask.setSchoolTerm (schoolTerm );
            }

            //添加班级关联属性
            if (schoolTeachingTask.getClassId() != null) {
                //查找教学任务所属班级
                SchoolClass schoolClass = schoolClassMapper.findById(schoolTeachingTask.getClassId());
                //赋值
                schoolTeachingTask.setSchoolClass(schoolClass);
            }



            //添加教室关联属性
            if (schoolTeachingTask.getClassRoomId() != null) {
                //查找教学任务所属教室
                SchoolClassRoom schoolClassRoom = schoolClassRoomMapper.findById(schoolTeachingTask.getClassRoomId());
                //赋值
                schoolTeachingTask.setSchoolClassRoom(schoolClassRoom);
            }

//            添加教师下教学任务教师数量值
//            schoolTeachingTask.setTeacherNumber(schoolTeachingTaskTeacherMapper.countAllTeacherByTeachingTaskId(schoolTeachingTask.getId()));
        }
    }

    @Override
    protected void afterSave(SchoolTeachingTask schoolTeachingTask) {
        //如果批量导入的教师不为空则循环添加
        if (schoolTeachingTask.getTeacherIds() != null){
            for(Long teacherId:schoolTeachingTask.getTeacherIds()){
                schoolTeachingTaskTeacherMapper.save(schoolTeachingTask.getId(),teacherId,1);
            }
        }else{//默认将当前登录教师设置为
        //获取当前登录信息
        SysUser sysUser = UserUtil.getLoginUser();
        System.out.println(sysUser);
        if (sysUser != null) {//如果当前登录账户存在
            //获取当前登录账户对应的教师信息
            SchoolTeacher schoolTeacher = schoolTeacherMapper.getTeacherByUserId(sysUser.getId());
            if ( schoolTeacher != null) {
                //查找教师信息下对应的课程
                schoolTeachingTaskTeacherMapper.save(schoolTeachingTask.getId(),schoolTeacher.getId(),1);
            }
        }
        }
    }

}
